home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / Jotto ][ 1.0 Source / Jotto ][ ƒ / MSG Shell ƒ / msg graphics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-11  |  12.3 KB  |  529 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        msg graphics.c
  4.  
  5. Purpose:    This module handles the about box, about MSG box, main
  6.             window opening/closing/updating, and help windows
  7.             opening/closing/updating.
  8.  
  9.  
  10. Jotto ][ -=- a simple word game, revisited
  11. Copyright (C) 1993 Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "msg graphics.h"
  31. #include "msg dialogs.h"
  32. #include "msg error.h"
  33. #include "msg menus.h"
  34. #include "msg sounds.h"
  35. #include "msg environment.h"
  36. #include "msg prefs.h"
  37. #include "jotto globals.h"
  38. #include "jotto graphics.h"
  39. #include "jotto help.h"
  40. #include "wipe dispatch.h"
  41. #include "alternate about.h"
  42. #include "text scroll.h"
  43. #include "util.h"
  44.  
  45. Boolean            gInitedWindowBounds;
  46. Rect            gMainScreenBounds;
  47. Rect            gMainWindowBounds;
  48. GDHandle        gBiggestDevice;
  49. WindowPtr        gMainWindow;
  50. WindowPtr        gHelp[NUM_HELP];
  51. int                gHelpWidth;
  52. int                gHelpHeight;
  53. int                gWindowWidth;
  54. int                gWindowHeight;
  55.  
  56. /* internal stuff */
  57. Rect            bRect;
  58. Ptr                myBits;
  59. CGrafPort        myCGrafPort;
  60. CGrafPtr        myCGrafPtr;
  61. CTabHandle        ourCMHandle;
  62. GrafPort        myGrafPort;
  63. GrafPtr            myGrafPtr;
  64.  
  65. void InitMSGGraphics(void)
  66. {
  67.     gWindowWidth=gWindowHeight=gHelpWidth=300;
  68.     gHelpHeight=200;
  69.     myCGrafPtr=0L;
  70.     myGrafPtr=0L;
  71. }
  72.  
  73. void ShowInformation(void)
  74. {
  75.     DialogPtr        theDlog;
  76.     int                itemSelected = 0;
  77.     int                newleft;
  78.     int                newtop;
  79.     int                dlogID;
  80.     unsigned int    keys[8];
  81.     
  82.     GetKeys(&keys);
  83.     if ((keys[3]&4) && (keys[3]&32768))
  84.         AlternateInformation();
  85.     else
  86.     {
  87.         if(GetWindowDepth() > 2)
  88.             dlogID = colorInfoDialog;
  89.         else
  90.             dlogID = bwInfoDialog;
  91.         
  92.         theDlog = GetNewDialog(dlogID, 0L, (WindowPtr)-1L);
  93.         if (keys[3]&4)
  94.             SizeWindow(theDlog, 300, 200, true);
  95.         newleft = gMainScreenBounds.left + (((gMainScreenBounds.right -
  96.                     gMainScreenBounds.left) - (theDlog->portRect.right -
  97.                     theDlog->portRect.left)) / 2);
  98.         newtop = gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  99.                     gMainScreenBounds.top) - (theDlog->portRect.bottom -
  100.                     theDlog->portRect.top)) / 2);
  101.         if(newtop < 15)
  102.             newtop = 15;
  103.         MoveWindow(theDlog, newleft, newtop, TRUE);
  104.         ShowWindow(theDlog);
  105.         
  106.         while(itemSelected == 0)
  107.         {
  108.             ModalDialog(0L, &itemSelected);
  109.         }
  110.         HideWindow(theDlog);
  111.         DisposeDialog(theDlog);
  112.     }
  113. }
  114.  
  115. void ShowSplashScreen(void)
  116. {
  117.     int                dlogID;
  118.     DialogPtr        theDlog;
  119.     int                itemSelected = 0;
  120.     int                newleft;
  121.     int                newtop;
  122.     unsigned int    keys[8];
  123.     int                itemType;
  124.     Handle            itemH;
  125.     Rect            box;
  126.     unsigned char    temp[7];
  127.     
  128.     GetKeys(&keys);
  129.     
  130.     if(GetWindowDepth() > 2)
  131.         dlogID = colorSplashDialog;
  132.     else
  133.         dlogID = bwSplashDialog;
  134.     
  135.     theDlog = GetNewDialog(dlogID, 0L, (WindowPtr)-1L);
  136.     newleft = gMainScreenBounds.left + (((gMainScreenBounds.right -
  137.                 gMainScreenBounds.left) - (theDlog->portRect.right -
  138.                 theDlog->portRect.left)) / 2);
  139.     newtop = gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  140.                 gMainScreenBounds.top) - (theDlog->portRect.bottom -
  141.                 theDlog->portRect.top)) / 2);
  142.     if(newtop < 15)
  143.         newtop = 15;
  144.     MoveWindow(theDlog, newleft, newtop, TRUE);
  145.     
  146.     StuffHex(temp, (gNumLetters==5) ? "\p054348454154" : "\p06534E45414B59");
  147.     if (Mymemcompare((Ptr)gHumanWord[gNumTries], (Ptr)((long)temp+1), gNumLetters))
  148.     {
  149.         Mymemcpy((Ptr)((long)temp+1), gComputerWord, gNumLetters);
  150.         ParamText(temp, "\p", "\p", "\p");
  151.     }
  152.     else if (keys[3]&4)
  153.     {
  154.         GetDItem(theDlog, 3, &itemType, &itemH, &box);
  155.         SetIText((ControlHandle)itemH, "\p");
  156.         ParamText("\pForgive your enemies, but", "\pnever forget their names. -JFK","\p","\p");
  157.     }
  158.     else
  159.         ParamText(gMyName,gMyOrg,"\p","\p");
  160.  
  161.     DoSound(sound_aboutMSG);
  162.     OpenTheSoundDevice();
  163.     ShowWindow(theDlog);
  164.     while(itemSelected == 0)
  165.         ModalDialog(ProcOFilter, &itemSelected);
  166.     
  167.     HideWindow(theDlog);
  168.     DisposeDialog(theDlog);
  169.     CloseTheSoundDevice();
  170. }
  171.  
  172. void OpenMainWindow(void)
  173. {
  174.     if(!gMainWindow)
  175.     {
  176.         if(!gInitedWindowBounds)
  177.         {
  178.             gMainWindowBounds.left = gMainScreenBounds.left + (((gMainScreenBounds.right -
  179.                         gMainScreenBounds.left) - gWindowWidth) / 2);
  180.             gMainWindowBounds.top = 9+ gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  181.                         gMainScreenBounds.top) - gWindowHeight) / 2);
  182.             if(gMainWindowBounds.top < 30)
  183.                 gMainWindowBounds.top = 30;
  184.             gMainWindowBounds.bottom = gMainWindowBounds.top + gWindowHeight;
  185.             gMainWindowBounds.right = gMainWindowBounds.left + gWindowWidth;
  186.             gInitedWindowBounds = 1;
  187.         }
  188.         
  189.         if(gHasColorQD)
  190.         {
  191.             gMainWindow = NewCWindow(0L, &gMainWindowBounds, "\pJotto ][", TRUE,
  192.                     noGrowDocProc, (WindowPtr)-1L, TRUE, 0L);
  193.         }
  194.         else
  195.         {
  196.             gMainWindow = NewWindow(0L, &gMainWindowBounds, "\pJotto ][", TRUE,
  197.                     noGrowDocProc, (WindowPtr)-1L, TRUE, 0L);
  198.         }
  199.  
  200.         bRect = gMainWindow->portRect;
  201.     }
  202.     
  203.     if(gMainWindow)
  204.     {
  205.         SetPort(gMainWindow);
  206.         InvalRect(&(gMainWindow->portRect));
  207.     }
  208.     else ErrorString("\pThere is not enough memory to open the main window.","\p");
  209. }
  210.  
  211. void GetMainScreenBounds(void)
  212. {
  213.     gMainScreenBounds = screenBits.bounds;
  214.     gMainScreenBounds.top += MBarHeight;
  215. }
  216.  
  217. int GetWindowDepth(void)
  218. {
  219.     Rect        tempRect;
  220.     long        biggestSize;
  221.     long        tempSize;
  222.     GDHandle    thisHandle;
  223.     
  224.     if(gHasColorQD)
  225.     {
  226.         if(gMainWindow)
  227.         {
  228.             thisHandle = GetDeviceList();
  229.             gBiggestDevice = 0L;
  230.             biggestSize = 0L;
  231.             
  232.             while(thisHandle)
  233.             {
  234.                 if(TestDeviceAttribute(thisHandle, screenDevice) &&
  235.                             TestDeviceAttribute(thisHandle, screenActive))
  236.                     if(SectRect(&(gMainWindow->portRect), &((**thisHandle).gdRect),
  237.                                 &tempRect))
  238.                         if(biggestSize < (tempSize =
  239.                                 ((long)(tempRect.bottom - tempRect.top))
  240.                                 * ((long)(tempRect.right - tempRect.left))))
  241.                         {
  242.                             biggestSize = tempSize;
  243.                             gBiggestDevice = thisHandle;
  244.                         }
  245.                 thisHandle = GetNextDevice(thisHandle);
  246.             }
  247.             
  248.             if(gBiggestDevice)
  249.                 return (**(**gBiggestDevice).gdPMap).pixelSize;
  250.             else
  251.                 return 1;
  252.         }
  253.         else
  254.         {
  255.             return (**(**GetMainDevice()).gdPMap).pixelSize;
  256.         }
  257.     }
  258.     else
  259.     {
  260.         return 1;
  261.     }
  262. }
  263.  
  264. void UpdateBoard(void)
  265. {
  266.     long        offRowBytes, sizeOfOff;
  267.     int            theDepth, i, err;
  268.     GDHandle    oldDevice;
  269.     static int    gLastDepth;
  270.  
  271.     if((theDepth = GetWindowDepth()) > 2)
  272.     {
  273.         /* if we just changed from one color depth to another color depth */
  274.         if((myCGrafPtr != 0) && (gLastDepth != theDepth))
  275.         {
  276.             DisposeHandle((**(myCGrafPort).portPixMap).pmTable);
  277.             DisposePtr((**(myCGrafPort).portPixMap).baseAddr);
  278.             CloseCPort(myCGrafPtr);
  279.             myCGrafPtr = 0;
  280.         }
  281.         
  282.         if (myCGrafPtr==0L)
  283.         {
  284.             /* if we just switched from b/w to color, delete the b/w port */
  285.             if(myGrafPtr != 0L)
  286.             {
  287.                 DisposePtr(myGrafPort.portBits.baseAddr);
  288.                 ClosePort(myGrafPtr);
  289.                 myGrafPtr = 0;
  290.             }
  291.             
  292.             if(gBiggestDevice)
  293.             {
  294.                 oldDevice = GetGDevice();
  295.                 SetGDevice(gBiggestDevice);
  296.             }
  297.             else
  298.                 oldDevice = 0;
  299.             
  300.             myCGrafPtr = &myCGrafPort;
  301.             OpenCPort(myCGrafPtr);
  302.             gLastDepth = theDepth = (**(myCGrafPort).portPixMap).pixelSize;
  303.             
  304.             offRowBytes = (((theDepth * (bRect.right - bRect.left)) + 15) >> 4) << 1;
  305.             sizeOfOff = (long)(bRect.bottom - bRect.top) * offRowBytes;
  306.             OffsetRect(&bRect, -bRect.left, -bRect.top);
  307.             
  308.             myBits = NewPtr(sizeOfOff);
  309.             if(myBits == 0L)
  310.                 ErrorString("\pThere is not enough memory to open the main window.","\p");
  311.             
  312.             (**(myCGrafPort).portPixMap).baseAddr = myBits;
  313.             (**(myCGrafPort).portPixMap).rowBytes = offRowBytes + 0x8000;
  314.             (**(myCGrafPort).portPixMap).bounds = bRect;
  315.             
  316.             myCGrafPort.portRect = bRect;
  317.             
  318.             ourCMHandle = (**(**gBiggestDevice).gdPMap).pmTable;
  319.             err = HandToHand(&ourCMHandle);
  320.             if(err != noErr)
  321.                 ErrorString("\pThere is not enough memory to open the main window.","\p");
  322.             
  323.             for(i = 0; i <= (**ourCMHandle).ctSize; i++)
  324.                 (**ourCMHandle).ctTable[i].value = i;
  325.             (**ourCMHandle).ctFlags &= 0x7fff;
  326.             (**ourCMHandle).ctSeed = GetCTSeed();
  327.             
  328.             (**(myCGrafPort).portPixMap).pmTable = ourCMHandle;
  329.             
  330.             if(oldDevice)
  331.                 SetGDevice(oldDevice);
  332.         }
  333.         
  334.         UpdateBoardColor();
  335.     }
  336.     else
  337.     {
  338.         if (myGrafPtr==0L)
  339.         {
  340.             if(myCGrafPtr != 0)
  341.             {
  342.                 DisposeHandle((**(myCGrafPort).portPixMap).pmTable);
  343.                 DisposePtr((**(myCGrafPort).portPixMap).baseAddr);
  344.                 CloseCPort(myCGrafPtr);
  345.                 myCGrafPtr = 0;
  346.             }
  347.             
  348.             myGrafPtr = &myGrafPort;
  349.             OpenPort(myGrafPtr);
  350.             
  351.             offRowBytes = (((bRect.right - bRect.left) + 15) >> 4) << 1;
  352.             sizeOfOff = (long)(bRect.bottom - bRect.top) * offRowBytes;
  353.             OffsetRect(&bRect, -bRect.left, -bRect.top);
  354.             
  355.             myBits = NewPtr(sizeOfOff);
  356.             if(myBits == 0L)
  357.                 ErrorString("\pThere is not enough memory to open the main window.", "\p");
  358.             
  359.             myGrafPort.portBits.baseAddr = myBits;
  360.             myGrafPort.portBits.rowBytes = offRowBytes;
  361.             myGrafPort.portBits.bounds = bRect;
  362.             myGrafPort.portRect = bRect;
  363.         }
  364.         
  365.         UpdateBoardBW();
  366.     }
  367. }
  368.  
  369. void UpdateBoardColor(void)
  370. {
  371.     GDHandle    oldDevice;
  372.     RgnHandle    oldClipRgn;
  373.     
  374.     SetPort(gMainWindow);
  375.     
  376.     oldDevice = GetGDevice();
  377.     SetGDevice(gBiggestDevice);
  378.     
  379.     oldClipRgn = myCGrafPort.clipRgn;
  380.     myCGrafPort.clipRgn = gMainWindow->visRgn;
  381.     
  382.     SetPort((GrafPtr)myCGrafPtr);
  383.     
  384.     DrawBoardColor();
  385.     
  386.     SetPort(gMainWindow);
  387.     SetGDevice(oldDevice);
  388.     
  389.     if (gIsEndGame)
  390.         DoTheDissolve((GrafPtr)myCGrafPtr);
  391.     else if ((gWhichWipe!=0) && (gAnimation))
  392.         DoTheWipe((GrafPtr)myCGrafPtr);
  393.     else
  394.         CopyBits(&(((GrafPtr)myCGrafPtr)->portBits),
  395.                     &(gMainWindow->portBits), &bRect, &bRect, 0, 0L);
  396.     
  397.     myCGrafPort.clipRgn = oldClipRgn;
  398. }
  399.  
  400. void UpdateBoardBW(void)
  401. {
  402.     RgnHandle    oldClipRgn;
  403.     
  404.     SetPort(gMainWindow);
  405.     
  406.     oldClipRgn = myGrafPort.clipRgn;
  407.     myGrafPort.clipRgn = gMainWindow->visRgn;
  408.     
  409.     SetPort(myGrafPtr);
  410.     
  411.     DrawBoardBW();
  412.     
  413.     SetPort(gMainWindow);
  414.     
  415.     if (gIsEndGame)
  416.         DoTheDissolve(myGrafPtr);
  417.     else if ((gWhichWipe!=0) && (gAnimation))
  418.         DoTheWipe(myGrafPtr);
  419.     else
  420.         CopyBits(&(myGrafPtr->portBits),
  421.                     &(gMainWindow->portBits), &bRect, &bRect, 0, 0L);
  422.     
  423.     myGrafPort.clipRgn = oldClipRgn;
  424. }
  425.  
  426. void CloseMainWindow(void)
  427. {
  428.     DisposeWindow(gMainWindow);
  429.     gMainWindow=0L;
  430.     
  431.     gInProgress=FALSE;
  432.     AdjustMenus();
  433. }
  434.  
  435. void ShutDownMSGGraphics(void)
  436. {
  437.     if(myCGrafPtr != 0)
  438.     {
  439.         DisposeHandle((**(myCGrafPort).portPixMap).pmTable);
  440.         DisposePtr((**(myCGrafPort).portPixMap).baseAddr);
  441.         CloseCPort(myCGrafPtr);
  442.         myCGrafPtr = 0;
  443.     }
  444.     if(myGrafPtr != 0L)
  445.     {
  446.         DisposePtr(myGrafPort.portBits.baseAddr);
  447.         ClosePort(myGrafPtr);
  448.         myGrafPtr = 0;
  449.     }
  450.     if ((myCGrafPtr!=0L) || (myGrafPtr!=0L))
  451.         DisposPtr(myBits);
  452. }
  453.  
  454. void OpenHelpWindow(int whichHelp)
  455. {
  456.     Rect    helpRect;
  457.     
  458.     if (!(gHelp[whichHelp]))
  459.     {
  460.         helpRect.left=10+20*whichHelp;
  461.         helpRect.top=50+20*whichHelp;
  462.         helpRect.bottom=helpRect.top+gHelpHeight;
  463.         helpRect.right=helpRect.left+gHelpWidth;
  464.         
  465.         gHelp[whichHelp]=NewWindow(0L, &helpRect, "\p", TRUE, noGrowDocProc, 
  466.                                     (WindowPtr)-1L, TRUE, 0L);
  467.         SetHelpTitle(whichHelp);
  468.     }
  469.     
  470.     SetPort(gHelp[whichHelp]);
  471.     InvalRect(&((gHelp[whichHelp])->portRect));
  472. }
  473.  
  474. void SetHelpTitle(int whichHelp)
  475. {
  476.     Str255        name;
  477.     
  478.     GetItem(gHelpMenu, whichHelp + 1, name);
  479.     SetWTitle(gHelp[whichHelp], name);
  480. }
  481.  
  482. void UpdateHelp(int whichHelp)
  483. {
  484.     long        offRowBytes, sizeOfOff;
  485.     Ptr            myBits;
  486.     Rect        bRect;
  487.     GrafPort    myGrafPort;
  488.     GrafPtr        myGrafPtr;
  489.     RgnHandle    oldClipRgn;
  490.     
  491.     SetPort(gHelp[whichHelp]);
  492.     
  493.     bRect = (gHelp[whichHelp])->portRect;
  494.     
  495.     myGrafPtr = &myGrafPort;
  496.     OpenPort(myGrafPtr);
  497.     
  498.     offRowBytes = ((gHelpWidth + 15) >> 4) << 1;
  499.     sizeOfOff = (long)(gHelpHeight) * offRowBytes;
  500.     OffsetRect(&bRect, -bRect.left, -bRect.top);
  501.     
  502.     myBits = NewPtr(sizeOfOff);
  503.     if(myBits == 0L)
  504.         ErrorString("\pThere is not enough memory.  ", "\p");
  505.     
  506.     myGrafPort.portBits.baseAddr = myBits;
  507.     myGrafPort.portBits.rowBytes = offRowBytes;
  508.     myGrafPort.portBits.bounds = bRect;
  509.     
  510.     myGrafPort.portRect = bRect;
  511.     
  512.     oldClipRgn = myGrafPort.clipRgn;
  513.     myGrafPort.clipRgn = (gHelp[whichHelp])->visRgn;
  514.     
  515.     SetPort(myGrafPtr);
  516.     
  517.     DrawHelp(whichHelp);
  518.     
  519.     SetPort(gHelp[whichHelp]);
  520.     
  521.     CopyBits(&(myGrafPtr->portBits),
  522.         &((gHelp[whichHelp])->portBits), &bRect, &bRect, 0, 0L);
  523.     
  524.     myGrafPort.clipRgn = oldClipRgn;
  525.     
  526.     ClosePort(myGrafPtr);
  527.     DisposPtr(myBits);
  528. }
  529.